﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Odebrecht.WpfControls
{
    [TemplateVisualState(GroupName = CustomDatePickerVisualStates.ButtonVisibilityGroup, Name = CustomDatePickerVisualStates.ButtonVisibleState)]
    [TemplateVisualState(GroupName = CustomDatePickerVisualStates.ButtonVisibilityGroup, Name = CustomDatePickerVisualStates.ButtonHiddenState)]
    public class CustomDatePicker : DatePicker
    {
        #region Fields

        public static readonly DependencyProperty IsAnimatedProperty = 
            DependencyProperty.Register("IsAnimated", typeof(bool), typeof(CustomDatePicker), new PropertyMetadata(true));
        public static readonly DependencyProperty IsButtonVisibleProperty = 
            DependencyProperty.Register("IsButtonVisible", typeof(bool), typeof(CustomDatePicker), new PropertyMetadata(true, new PropertyChangedCallback(IsButtonVisibleOnPropertyChangedCallback)));

        #endregion Fields

        #region Constructors

        static CustomDatePicker()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomDatePicker), new FrameworkPropertyMetadata(typeof(CustomDatePicker)));
        }

        #endregion Constructors

        #region Properties

        [Browsable(true)]
        [Category("Odebrecht")]
        [Description("IsAnimated")]
        public bool IsAnimated
        {
            get { return (bool)GetValue(IsAnimatedProperty); }
            set { SetValue(IsAnimatedProperty, value); }
        }

        [Browsable(true)]
        [Category("Odebrecht")]
        [Description("Indica que o botão está visível.")]
        public bool IsButtonVisible
        {
            get { return (bool)GetValue(IsButtonVisibleProperty); }
            set { SetValue(IsButtonVisibleProperty, value); }
        }

        #endregion Properties

        #region Methods

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (!IsButtonVisible)
            {
                VisualStateManager.GoToState(this, CustomDatePickerVisualStates.ButtonHiddenState, false);
            }
        }

        protected virtual void IsButtonVisibleOnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var value = Convert.ToBoolean(e.NewValue);
            SetButtonVisibilityState(value);
        }

        private static void IsButtonVisibleOnPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var customDatePicker = sender as CustomDatePicker;
            customDatePicker.Dispatcher.Invoke(new Action(() =>
            {
                customDatePicker.IsButtonVisibleOnPropertyChanged(sender, e);
            }));
        }

        private void SetButtonVisibilityState(bool isVisible)
        {
            if (isVisible)
            {
                VisualStateManager.GoToState(this, CustomDatePickerVisualStates.ButtonVisibleState, IsAnimated);
            }
            else
            {
                VisualStateManager.GoToState(this, CustomDatePickerVisualStates.ButtonHiddenState, IsAnimated);
            }
        }

        #endregion Methods
    }
}